package com.example.schooltest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.schooltest.entity.MyObjectBox;
import com.example.schooltest.entity.UserIm;

import java.util.List;

import io.objectbox.BoxStore;

public class LoginActivity extends Activity {

    private Button button1;
    private Button button;
    private EditText edit;
    private EditText edit1;
    private RadioButton radioButton;
    private boolean flag = true;
    public static BoxStore boxStore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        if (boxStore == null) {
            boxStore = MyObjectBox.builder().androidContext(this).build();
        }

        /*
         * 初始化
         * */
        button = (Button) findViewById(R.id.bu);
        button1 = (Button) findViewById(R.id.bul);
        edit = (EditText) findViewById(R.id.edit);
        edit1 = (EditText) findViewById(R.id.edit1);
        radioButton = (RadioButton) findViewById(R.id.rb);


        showUser();
        goRegistryUser();
        keepUserYN();
        goLoginMain();
        List<UserIm> list = boxStore.boxFor(UserIm.class).getAll();
        for (UserIm u : list) {
            Log.d("666", u.getName() + "   " + u.getId() + "   " + u.getPassWard() + "   " + u.getSex() + "   " + u.getNewUser() + "   " + u.getShowUser());
        }


    }


    private void goLoginMain() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ifLogin();
            }
        });
    }

    private boolean keepUserYN() {
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<UserIm> list = boxStore.boxFor(UserIm.class).getAll();
                /*
                 * 保存密码
                 * */
                if (flag) {
                    radioButton.setChecked(true);
                    flag = false;
                } else {
                    /*
                     * 不保存密码
                     * */
                    radioButton.setChecked(false);
                    flag = true;
                }
            }
        });
        return radioButton.isChecked();
    }

    private void goRegistryUser() {
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegistryActivity.class));
            }
        });
    }

    private void showUser() {
        if (boxStore.boxFor(UserIm.class).getAll().size() > 0) {
            List<UserIm> list = boxStore.boxFor(UserIm.class).getAll();
            UserIm user = list.get(list.size() - 1);
            if ("true".equals(user.getNewUser())) {
                edit.setText(user.getName());
                edit1.setText(user.getPassWard());
                user.setNewUser("false");
                boxStore.boxFor(UserIm.class).put(user);
            } else {
                for (UserIm user1 : list) {
                    if ("true".equals(user1.getShowUser())) {
                        radioButton.setChecked(true);
                        edit.setText(user1.getName());
                        edit1.setText(user1.getPassWard());
                    }
                }
            }
        }
    }


    private void ifLogin() {
        if ("".equals(edit.getText().toString()) || "".equals(edit1.getText().toString())) {
            Toast.makeText(LoginActivity.this, "账号或密码未输入", Toast.LENGTH_SHORT).show();

        } else {
            boolean boole = false;
            List<UserIm> list = boxStore.boxFor(UserIm.class).getAll();
            for (UserIm user1 : list) {
                if (edit.getText().toString().equals(user1.getName()) && edit1.getText().toString().equals(user1.getPassWard())) {
                    if (keepUserYN() == true) {
                        user1.setShowUser("true");
                    }
                    user1.setIsLogin(UserIm.LOGIN);
                    boxStore.boxFor(UserIm.class).put(user1);
                    boole = true;
                } else {
                    user1.setShowUser("false");
                    user1.setIsLogin(UserIm.NOLOGIN);
                    boxStore.boxFor(UserIm.class).put(user1);
                }
            }
            if (boole) {
                Toast.makeText(LoginActivity.this, "登录成功，即将跳转", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(LoginActivity.this, LoginMainActivity.class));
            } else {
                Toast.makeText(LoginActivity.this, "登录失败，请检查账号密码", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /*
    * 删除全部用户
    * */
    private void deleteAllUser() {
        List<UserIm> list = boxStore.boxFor(UserIm.class).getAll();
        for (UserIm u : list) {
            boxStore.boxFor(UserIm.class).remove(u);
        }
    }

}